package com.fy.common.util;

import com.fy.common.enums.BaseEnum;
import com.fy.common.enums.ErrorType;
import com.fy.common.exception.BusinessException;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.Arrays;
import java.util.Optional;


public class EnumUtils {
    private static Logger logger = LoggerFactory.getLogger(EnumUtils.class);

    @NonNull
    public static final <T extends BaseEnum> T findByValidatorValue(Class<T> enumClass, Object validatorValue) {
        Optional<T> typeEnumOptional = Arrays.stream(enumClass.getEnumConstants()).filter(type -> type
                .getValidatorValue().equals(validatorValue)).findFirst();
        if (typeEnumOptional.isPresent()) {
            return typeEnumOptional.get();
        }
        logger.error("根据值[{}]未找到[{}]中对应的枚举类型",validatorValue,enumClass.getName());
        throw new BusinessException(ErrorType.SYSTEM_ERROR);

    }
}
